﻿//-----------------------------------------------------------------------
// <copyright file="IdentityBusinessBase.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System;
using GateShift.Common.Repository.Interfaces.DataInterfaces.Base;
using GateShift.Common.Repository.Interfaces.Factories;
using GateShift.Common.Repository.Interfaces.Repositories.Base;

#endregion

namespace GateShift.Common.Repository.Business
{
    /// <summary>
    /// Business base class for repository based objects with an Identity key
    /// </summary>
    /// <typeparam name="D">IIdentityData based source for data</typeparam>
    /// <typeparam name="R">IRepository based source for actions</typeparam>
    public abstract class IdentityBusinessBase<D, R>
        : BusinessBase<D, R>
        where D : class, IIdentityData
        where R : class, IIdentityRepository<D>
    {
        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of the IdentityBusinessBase class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="data">Data for backing the business object</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory or Data are null</exception>
        protected IdentityBusinessBase(IRepositoryFactory factory, D data)
            : base(factory, data)
        {
        }

        /// <summary>
        /// Initializes a new instance of the IdentityBusinessBase class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        protected IdentityBusinessBase(IRepositoryFactory factory)
            : base(factory)
        {
        }

        /// <summary>
        /// Initializes a new instance of the IdentityBusinessBase class
        /// </summary>
        /// <param name="factory">Factory for retrieving the Repository from</param>
        /// <param name="ID">Identity of the data to retrieve</param>
        /// <exception cref="ArgumentNullException">Thrown if Factory is null</exception>
        protected IdentityBusinessBase(IRepositoryFactory factory, Identity ID)
            : base(factory)
        {
            if (factory == null)
            {
                throw new ArgumentNullException("factory", "Cannot retrieve data without a factory");
            }

            Repository = RetrieveRepository(factory);
            Data = Repository.Retrieve(ID);
            Factory = factory;

            if (Data == null)
            {
                throw new InvalidOperationException(string.Format("Unable to retrieve data from database with ID of {0} for type {1}", ID, typeof(D)));
            }
        }

        #endregion

        #region Public ReadOnly Properties

        /// <summary>
        /// Gets the ID for the current object
        /// </summary>
        public Identity ID
        {
            get
            {
                return Data.ID;
            }
        }

        #endregion
    }
}